﻿package
{
	import com.citrusengine.core.CitrusEngine;
	import com.citrusengine.core.State;
	import com.citrusengine.math.MathVector;
	import com.citrusengine.objects.CitrusSprite;
	import com.citrusengine.objects.PhysicsObject;
	import com.citrusengine.objects.platformer.Baddy;
	import com.citrusengine.view.spriteview.SpriteArt;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.StageQuality;
	import flash.net.URLRequest;
	import com.citrusengine.objects.platformer.Hero;
	import com.citrusengine.physics.Box2D;
	import com.citrusengine.utils.ObjectMaker;
	import com.citrusengine.objects.platformer.Coin;
	import com.citrusengine.objects.platformer.Platform;
	import flash.ui.Keyboard;


	public class Level extends State
	{
		private var levelData:XML;
		private var box2D:Box2D = new Box2D("Box2D");
		private var _ce: CitrusEngine;
		private var _hero:Hero;
		
		public function Level(_levelData:XML) 
		{
			super();
					
			
			//recebe o XML por parâmetro
			levelData = _levelData;
		}
		
		override public function initialize():void
		{
			super.initialize();
			
			_ce = CitrusEngine.getInstance();
			
			
			//Create Box2D
			
			add(box2D);
			
			//Debbuging do box2D
			box2D.visible = false;
			
			stage.quality = StageQuality.BEST;
			
			
			//Cria e desenha na tela o level.
			if (levelData)
				ObjectMaker.FromLevelArchitect(levelData);	

			
			_hero = getFirstObjectByType(Hero) as Hero;
			
			if (_hero)
			{
				view.cameraTarget = _hero;
				view.cameraOffset = new MathVector(stage.stageWidth / 2, stage.stageHeight / 2);
				view.cameraEasing = new MathVector(0.08, 0.08);
			}
		}
		
		
		override public function update(timeDelta:Number):void
		{
			super.update(timeDelta);
			
			verifyDebugKeys();
		}
		
		private function verifyDebugKeys():void
		{
			if (_ce.input.isDown(Keyboard.F2))
			{
				box2D.visible = true;
			}
			
			if (_ce.input.isDown(Keyboard.F3))
			{
				box2D.visible = false;
			}
		}
	
	}

}